use clap::Parser;
use std::net::ToSocketAddrs;

mod commands;
use commands::Commands;

#[derive(Parser, Debug)]
#[command(version, author, about, long_about = None)]
pub struct Param {
    /// 链接目标
    #[arg(short, default_value = "any")]
    target: String,
    /// 日志级别
    #[arg(short, default_value = "3", value_parser = clap::value_parser!(u16).range(0..6))]
    level: u16,
    /// 启动server
    #[arg(short, default_value = "false")]
    master: bool,
    /// 服务器地址
    #[arg(short, value_parser = check_server_adder, default_value = "127.0.0.1:9000")]
    server: String,

    #[command(subcommand)]
    command: Option<Commands>,
}

fn check_server_adder(s: &str) -> Result<String, String> {
    match s.to_socket_addrs() {
        Ok(_adder) => Ok(s.to_owned()),
        Err(_) => Ok("127.0.0.1:9000".to_owned()),
    }
}

impl Param {
    pub fn get_level(&self) -> u16 {
        self.level
    }

    pub fn is_server(&self) -> bool {
        self.master
    }

    pub fn get_server_add(&self) -> String {
        self.server.clone()
    }
}
